package com.coffee.wakeonlan.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.coffee.wakeonlan.entity.Machine;
import com.coffee.wakeonlan.security.MyUserDetails;
import com.coffee.wakeonlan.service.MachineService;
import com.coffee.wakeonlan.utils.MulticastUtil;
import com.coffee.wakeonlan.utils.WakeOnLan;

@Controller
@RequestMapping("/machine")
public class MachineController {
	
	@Autowired
	private MachineService machineService;
	@Autowired
	private MulticastUtil multicastUtil;
	
	@RequestMapping(value="/list",method = RequestMethod.GET)
    public ModelAndView list() {
		MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext()
			    .getAuthentication()
			    .getPrincipal();
		ModelAndView mv=new ModelAndView("list");
		List<Machine> results= machineService.findByUserId(userDetails.getId());
		mv.addObject("results", results);
        return mv;
    }
	@RequestMapping(value="/add",method = RequestMethod.GET)
	public String add() {
		return "add";
	}
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public String addForm(Machine machine) {
		MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext()
			    .getAuthentication()
			    .getPrincipal();
		machine.setUserId(userDetails.getId());
		machine.setMac(machine.getMac().replaceAll("[\\-: ]", "").toUpperCase());
		machineService.add(machine);
		return "redirect:/machine/list";
	}
	@RequestMapping(value="/wake/{machineId}",method = RequestMethod.POST)
	@ResponseBody
	public boolean wake(@PathVariable Long machineId) {
		MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext()
			    .getAuthentication()
			    .getPrincipal();
		Machine machine=machineService.get(machineId);
		if (machine!=null&&machine.getUserId()==userDetails.getId()) {
			return WakeOnLan.wake(machine.getMac());
		}
		return false;
	}
	@RequestMapping(value="/off/{machineId}",method = RequestMethod.POST)
	@ResponseBody
	public boolean off(@PathVariable Long machineId) {
		MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext()
				.getAuthentication()
				.getPrincipal();
		Machine machine=machineService.get(machineId);
		if (machine!=null&&machine.getUserId()==userDetails.getId()) {
			
			if (StringUtils.hasText(machine.getIp())&&StringUtils.hasText(machine.getOsType())) {
				
				if ((machine.getOsType().toLowerCase()).contains("windows")) {
					multicastUtil.sendCommond(machine.getIp(), "shutdown -s -t 00");
				}else if ((machine.getOsType().toLowerCase()).contains("linux")){
					multicastUtil.sendCommond(machine.getIp(), "shutdown -h now");
				}
			}
			return true;
		}
		return false;
	}
	@RequestMapping(value="/delete/{machineId}",method = RequestMethod.POST)
	@ResponseBody
	public boolean delete(@PathVariable Long machineId) {
		MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext()
				.getAuthentication()
				.getPrincipal();
		Machine machine=machineService.get(machineId);
		if (machine!=null&&machine.getUserId()==userDetails.getId()) {
			machineService.delete(machine);
			return true;
		}
		return false;
	}
}
